package mh.demo.jdk.lang;

import java.util.concurrent.TimeUnit;

/**
 * 《Java并发编程艺术》书中的守护线程demo
 * Java 虚拟机退出时 Daemon 线程中的 finally 块并不一定会执行
 */
public class ThreadDaemonDemo {
    public static void main(String[] args) {
        Thread thread = new Thread(new DaemonRunner(), "DaemonRunner");
        thread.setDaemon(true);
        thread.start();
    }
    static class DaemonRunner implements Runnable {
        @Override
        public void run() {
            try {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                }
            } finally {
                System.out.println("DaemonThread finally run.");
            }
        }
    }

}
